﻿using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using TTQT.TTQT_Code;

/// <summary>
/// Summary description for HangHoa
/// </summary>
public class HangHoa
{
    public string id = "";
    public string maHang = "";
    public string maCN = "";
    public string maNhom = "";
    public string tenHang = "";
    public string donViTinh = "";
    public string tonThap = "";
    public string tonCao = "";
    public string giaNhap = "";
    public string giaBan = "";
    public string khuyenMai = "";
    public string giaBanThucTe = "";
    public string giaBanBuon = "";
    public string anh = "";
    public string moTa = "";
    public string ghiChu = "";
    public string active = "1";
    public string laPhuKien = "";
    public string baoHanh = "";
    public string ngaySanXuat = "";
    public string hanSuDung = "";
    public string thue = "";
    public string barcode_New = "";
    public string barcode_Old = "";
    public string barcode_Date = "";

    public int insert()
    {
        int kq = 0;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand cmd = new SqlCommand("DM_Hang_Insert", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@maHang", maHang);
        cmd.Parameters.AddWithValue("@maCN", maCN);
        cmd.Parameters.AddWithValue("@maNhom", maNhom);
        cmd.Parameters.AddWithValue("@tenHang", tenHang);
        cmd.Parameters.AddWithValue("@donViTinh", donViTinh);
        cmd.Parameters.AddWithValue("@tonThap", tonThap);
        cmd.Parameters.AddWithValue("@tonCao", tonCao);
        cmd.Parameters.AddWithValue("@giaNhap", giaNhap);
        cmd.Parameters.AddWithValue("@giaBan", giaBan);
        cmd.Parameters.AddWithValue("@khuyenMai", khuyenMai);
        cmd.Parameters.AddWithValue("@giaBanThucTe", giaBanThucTe);
        cmd.Parameters.AddWithValue("@giaBanBuon", giaBanBuon);
        cmd.Parameters.AddWithValue("@anh", anh);
        cmd.Parameters.AddWithValue("@moTa", moTa);
        cmd.Parameters.AddWithValue("@ghiChu", ghiChu);
        cmd.Parameters.AddWithValue("@active", active);
        cmd.Parameters.AddWithValue("@laPhuKien", laPhuKien);
        cmd.Parameters.AddWithValue("@baoHanh", baoHanh);
        cmd.Parameters.AddWithValue("@ngaySanXuat", ngaySanXuat);
        cmd.Parameters.AddWithValue("@hanSuDung", hanSuDung);
        cmd.Parameters.AddWithValue("@thue", thue);
        cmd.Parameters.AddWithValue("@barcode_New", barcode_New);
        cmd.Parameters.AddWithValue("@barcode_Old", barcode_Old);
        cmd.Parameters.AddWithValue("@barcode_Date", barcode_Date);
        try
        {
            conn.Open();
            kq = TTQTCommon.ToInt32(cmd.ExecuteScalar(), 0);
        }
        catch (Exception exxx)
        {
            CommonDAO d=new CommonDAO();
            d.LogNgoaiLe_Insert("InsertHang", "InsertHang", "", exxx.ToString());
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            cmd.Dispose();
        }
        return kq;
    }
    public int update()
    {
        int kq = 0;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand cmd = new SqlCommand("DM_Hang_Update", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Id", id);
        cmd.Parameters.AddWithValue("@maHang", maHang);
        cmd.Parameters.AddWithValue("@maCN", maCN);
        cmd.Parameters.AddWithValue("@maNhom", maNhom);
        cmd.Parameters.AddWithValue("@tenHang", tenHang);
        cmd.Parameters.AddWithValue("@donViTinh", donViTinh);
        cmd.Parameters.AddWithValue("@tonThap", tonThap);
        cmd.Parameters.AddWithValue("@tonCao", tonCao);
        cmd.Parameters.AddWithValue("@giaNhap", giaNhap);
        cmd.Parameters.AddWithValue("@giaBan", giaBan);
        cmd.Parameters.AddWithValue("@khuyenMai", khuyenMai);
        cmd.Parameters.AddWithValue("@giaBanThucTe", giaBanThucTe);
        cmd.Parameters.AddWithValue("@giaBanBuon", giaBanBuon);
        cmd.Parameters.AddWithValue("@anh", anh);
        cmd.Parameters.AddWithValue("@moTa", moTa);
        cmd.Parameters.AddWithValue("@ghiChu", ghiChu);
        cmd.Parameters.AddWithValue("@active", active.Equals("True")? "1":"0");
        cmd.Parameters.AddWithValue("@laPhuKien", laPhuKien);
        cmd.Parameters.AddWithValue("@baoHanh", baoHanh);
        cmd.Parameters.AddWithValue("@ngaySanXuat", ngaySanXuat);
        cmd.Parameters.AddWithValue("@hanSuDung", hanSuDung);
        cmd.Parameters.AddWithValue("@thue", thue);
        cmd.Parameters.AddWithValue("@barcode_New", barcode_New);
        cmd.Parameters.AddWithValue("@barcode_Old", barcode_Old);
        cmd.Parameters.AddWithValue("@barcode_Date", barcode_Date);
        try
        {
            conn.Open();
            kq = cmd.ExecuteNonQuery();
        }
        catch (Exception exxx)
        {
            CommonDAO d = new CommonDAO();
            d.LogNgoaiLe_Insert("EditHang", "EditHang", "", exxx.ToString());
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            cmd.Dispose();
        }
        return kq;
    }
    public int getByID()
    {
        int kq = 0;
        DataTable result = null;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("DM_Hang_GetById", conn);
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@id", id);
        try
        {
            result = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(result);
            if (result.Rows.Count > 0)
            {
                maHang = result.Rows[0]["MaHang"].ToString();
                maCN = result.Rows[0]["MaCN"].ToString();
                maNhom = result.Rows[0]["MaNhom"].ToString();
                tenHang = result.Rows[0]["TenHang"].ToString();
                donViTinh = result.Rows[0]["DonViTinh"].ToString();
                tonThap = result.Rows[0]["TonThap"].ToString();
                tonCao = result.Rows[0]["TonCao"].ToString();
                giaNhap = result.Rows[0]["GiaNhap"].ToString();
                giaBan = result.Rows[0]["GiaBan"].ToString();
                khuyenMai = result.Rows[0]["KhuyenMai"].ToString();
                giaBanThucTe = result.Rows[0]["GiaBanThucTe"].ToString();
                giaBanBuon = result.Rows[0]["GiaBanBuon"].ToString();
                anh = result.Rows[0]["Anh"].ToString();
                moTa = result.Rows[0]["MoTa"].ToString();
                ghiChu = result.Rows[0]["GhiChu"].ToString();
                active = result.Rows[0]["Active"].ToString();
                laPhuKien = result.Rows[0]["LaPhuKien"].ToString();
                baoHanh = result.Rows[0]["BaoHanh"].ToString();
                ngaySanXuat = result.Rows[0]["NgaySanXuat"].ToString();
                hanSuDung = result.Rows[0]["HanSuDung"].ToString();
                thue = result.Rows[0]["Thue"].ToString();
                barcode_New = result.Rows[0]["Barcode_New"].ToString();
                barcode_Old = result.Rows[0]["Barcode_Old"].ToString();
                barcode_Date = result.Rows[0]["Barcode_Date"].ToString();
                kq = 1;
            }
        }
        catch (Exception exx)
        {
            CommonDAO d = new CommonDAO();
            d.LogNgoaiLe_Insert("EditHang", "EditHang", "", exx.ToString());
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return kq;
    }
    public int getByMaHang()
    {
        int kq = 0;
        DataTable result = null;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("DM_Hang_GetByMaHang", conn);
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@MaHang", maHang);
        try
        {
            result = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(result);
            if (result.Rows.Count > 0)
            {
                maHang = result.Rows[0]["MaHang"].ToString();
                maCN = result.Rows[0]["MaCN"].ToString();
                maNhom = result.Rows[0]["MaNhom"].ToString();
                tenHang = result.Rows[0]["TenHang"].ToString();
                donViTinh = result.Rows[0]["DonViTinh"].ToString();
                tonThap = result.Rows[0]["TonThap"].ToString();
                tonCao = result.Rows[0]["TonCao"].ToString();
                giaNhap = result.Rows[0]["GiaNhap"].ToString();
                giaBan = result.Rows[0]["GiaBan"].ToString();
                khuyenMai = result.Rows[0]["KhuyenMai"].ToString();
                giaBanThucTe = result.Rows[0]["GiaBanThucTe"].ToString();
                giaBanBuon = result.Rows[0]["GiaBanBuon"].ToString();
                anh = result.Rows[0]["Anh"].ToString();
                moTa = result.Rows[0]["MoTa"].ToString();
                ghiChu = result.Rows[0]["GhiChu"].ToString();
                active = result.Rows[0]["Active"].ToString();
                laPhuKien = result.Rows[0]["LaPhuKien"].ToString();
                baoHanh = result.Rows[0]["BaoHanh"].ToString();
                ngaySanXuat = result.Rows[0]["NgaySanXuat"].ToString();
                hanSuDung = result.Rows[0]["HanSuDung"].ToString();
                thue = result.Rows[0]["Thue"].ToString();
                barcode_New = result.Rows[0]["Barcode_New"].ToString();
                barcode_Old = result.Rows[0]["Barcode_Old"].ToString();
                barcode_Date = result.Rows[0]["Barcode_Date"].ToString();
                kq = 1;
            }
        }
        catch (Exception exx)
        {
            CommonDAO d = new CommonDAO();
            d.LogNgoaiLe_Insert("GetByMaHang", "GetByMaHang", "", exx.ToString());
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return kq;
    }
}
